﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Models;
using System.Collections;
using System.Data.Objects;
using System.Text;
using FreedomERP.Comm;
using FreedomERP.Comm.Utils;

namespace FreedomERP.Dao.Inv.Impl
{
    public class InvReceiveOrderDAOImpl:IInvReceiveOrderDAO
    {
        FreedomERPEntities dbEntities = new FreedomERPEntities();

        /// <summary>
        /// 根据条件获取收货单列表
        /// 作者：梁建东
        /// </summary>
        public List<InvReceiveOrder> QueryReceiveOrderPaggingByParam(PagingParams paggingParams, out int totalCount)
        {
            Hashtable filters = paggingParams.filters;

            //where
            List<ObjectParameter> paramList = new List<ObjectParameter>();
            StringBuilder sbWhere = new StringBuilder();
            if (filters["FNumberParam"] != null && !filters["FNumberParam"].ToString().Equals(""))
            {
                sbWhere.Append("it.FNumber like @FNumberParam and ");
                ObjectParameter param = new ObjectParameter("FNumberParam", "%"+filters["FNumberParam"].ToString()+"%");
                paramList.Add(param);
            }
            if (filters["FBillDateFrom"] != null && !filters["FBillDateFrom"].ToString().Equals(""))
            {
                sbWhere.Append("it.FBillDate between @FBillDateFrom and @FBillDateTo and ");
                ObjectParameter param = new ObjectParameter("FBillDateFrom", DateUtils.ParseStringToDateTime(filters["FBillDateFrom"].ToString()));
                paramList.Add(param);
                string FBillDateTo = "";
                if (filters["FBillDateTo"] != null && !filters["FBillDateTo"].ToString().Equals(""))
                {
                    FBillDateTo = filters["FBillDateTo"].ToString();
                }
                else 
                {
                    FBillDateTo = DateUtils.GetCurDateFormat("yyyy/MM/dd HH:mm:ss");
                }
                ObjectParameter paramT = new ObjectParameter("FBillDateTo", DateUtils.ParseStringToDateTime(FBillDateTo));
                paramList.Add(paramT);
            }
            if (filters["FStatusId"] != null && !filters["FStatusId"].ToString().Equals(""))
            {
                sbWhere.Append("it.FStatusId=@FStatusId and ");
                ObjectParameter param = new ObjectParameter("FStatusId", filters["FStatusId"].ToString());
                paramList.Add(param);
            }

            sbWhere.Append("it.FOrgunitId=@orgunitParam and ");
            ObjectParameter paramOrg = new ObjectParameter("orgunitParam", filters["orgunitParam"].ToString());
            paramList.Add(paramOrg);

            sbWhere.Append("1=1");

            //order by
            StringBuilder sbOrder = new StringBuilder();
            sbOrder.Append("it." + paggingParams.Sort + " " + paggingParams.Dir);

            ObjectParameter[] objParameters = new ObjectParameter[paramList.Count];
            int c = 0;
            foreach (ObjectParameter paramTemp in paramList)
            {
                objParameters[c] = paramTemp;
                c++;
            }

            var query = dbEntities.InvReceiveOrder.Where(sbWhere.ToString(), objParameters).OrderBy(sbOrder.ToString());
            totalCount = query.Count();

            List<InvReceiveOrder> list = query.Skip(paggingParams.Start).Take(paggingParams.Limit).ToList();

            return list;
        }

        /// <summary>
        /// 根据FId获取收货单列表
        /// 作者：梁建东
        /// </summary>
        public InvReceiveOrder GetReceiveOrderByFId(String FId)
        {
            InvReceiveOrder receiveOrder = dbEntities.InvReceiveOrder.FirstOrDefault(ro => ro.FId == FId);
            return receiveOrder;
        }

        /// <summary>
        /// 添加收货单
        /// 作者：梁建东
        /// </summary>
        public void AddReceiveOrder(InvReceiveOrder receiveOrder)
        {
            dbEntities.InvReceiveOrder.AddObject(receiveOrder);
        }

        /// <summary>
        /// 删除收货单
        /// 作者：梁建东
        /// </summary>
        public void DeleteReceiveOrder(InvReceiveOrder receiveOrder) 
        {
            dbEntities.InvReceiveOrder.DeleteObject(receiveOrder);
        }

        /// <summary>
        /// 保存更改
        /// 作者：梁建东
        /// </summary>
        public void Save()
        {
            dbEntities.SaveChanges();
        }
    }
}